#include "MyArray.h"
#include <iostream>

MyArray::MyArray(int size) : N(size) {
	if (size > 0) {
		p = new int[size];
		for (int i = 0; i < size; i++) {
			p[i] = 0;
		}
	}
	else {
		p = nullptr;
	}
}

MyArray::MyArray(const MyArray& other) : N(other.N) {
	if (other.p != nullptr && N > 0) {
		p = new int[N];
		for (int i = 0; i < N; i++) {
			p[i] = other.p[i];
		}
	}
	else {
		p = nullptr;
	}
}

MyArray::~MyArray() {
	if (p != nullptr) {
		delete[] p;
		p = nullptr;
		N = 0;
	}
}

MyArray MyArray::merge(const MyArray& other) const {
	int newSize = N + other.N;
	MyArray result(newSize); 

	for (int i = 0; i < N; i++) {
		result.p[i] = p[i];
	}

	for (int i = 0; i < other.N; i++) {
		result.p[N + i] = other.p[i];
	}
	return result;
}

MyArray MyArray::add(const MyArray& other) const {
	int minSize = (N < other.N) ? N : other.N;
	MyArray result(minSize);

	for (int i = 0; i < minSize; i++) {
		result.p[i] = p[i] + other.p[i];
	}
	return result;
}

MyArray MyArray::subtract(const MyArray& other) const {
	int minSize = (N < other.N) ? N : other.N;
	MyArray result(minSize);

	for (int i = 0; i < minSize; i++) {
		result.p[i] = p[i] - other.p[i];
	}
	return result;
}

MyArray MyArray::multiply(const MyArray& other) const {
	int minSize = (N < other.N) ? N : other.N;
	MyArray result(minSize);

	for (int i = 0; i < minSize; i++) {
		result.p[i] = p[i] * other.p[i];
	}
	return result;
}

MyArray MyArray::divide(const MyArray& other) const {
	int minSize = (N < other.N) ? N : other.N;
	MyArray result(minSize);

	for (int i = 0; i < minSize; i++) {
		if (other.p[i] != 0) {
			result.p[i] = p[i] / other.p[i];
		}
		else {
			result.p[i] = 0; 
		}
	}
	return result;
}